# Maintainer: Daniel M. Capella <polyzen@archlinux.org>

pkgname=jedi-language-server
pkgver=0.40.0
pkgrel=2
pkgdesc='Language server for Jedi'
arch=('any')
url=https://github.com/pappasam/jedi-language-server
license=('MIT')
depends=('python-docstring-to-markdown' 'python-jedi' 'python-pydantic'
         'python-pygls')
makedepends=('python-build' 'python-installer' 'python-poetry-core')
checkdepends=('python-lsp-jsonrpc' 'python-pyhamcrest' 'python-pytest')
source=("$url/archive/v$pkgver/$pkgname-$pkgver.tar.gz"
        'jedi-ls-migrate-to-python-lsp-jsonrpc.patch::https://github.com/pappasam/jedi-language-server/commit/5e6cdca4936271f0f85d06d4da59718c67efae9c.patch')
b2sums=('956b28824557e9910feac8274d3441ec7428481a58df253f0f066c4834882a115eaea21d4c026a7e1a023305fa99e2547f3df930971ac3c44be84eaae743e4d0'
        '67d4688ce37b31bdcfd0192e118a9845f8c722f1bb6b73a175eb5fa78197fb6b9e62f62bd2b52084a6fa4f035a31168a37d17d17932af91a9e613ae08039efda')

prepare() {
  cd $pkgname-$pkgver
  patch --forward --strip=1 --input=../jedi-ls-migrate-to-python-lsp-jsonrpc.patch

  # Remove include list https://github.com/pypa/wheel/issues/92
  sed -i '/include = \["README.md"\]/d' pyproject.toml
}

build() {
  cd $pkgname-$pkgver
  python -m build --wheel --skip-dependency-check --no-isolation
}

check() {
  cd $pkgname-$pkgver
  mkdir -p temp
  local site_packages=$(python -c "import site; print(site.getsitepackages()[0])")
  python -m installer --destdir=temp dist/*.whl
  PATH="$PWD/temp/usr/bin:$PATH" PYTHONPATH="$PWD/temp/$site_packages" pytest tests
}

package() {
  cd $pkgname-$pkgver
  python -m installer --destdir="$pkgdir" dist/*.whl

  # Symlink license file
  local site_packages=$(python -c "import site; print(site.getsitepackages()[0])")
  install -d "$pkgdir"/usr/share/licenses/$pkgname
  ln -s "$site_packages"/${pkgname//-/_}-$pkgver.dist-info/LICENSE \
    "$pkgdir"/usr/share/licenses/$pkgname/LICENSE
}
